package com.yohann.exam_service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.exam_service.entity.Listening;
import com.yohann.exam_service.entity.Reading;
import com.yohann.exam_service.entity.vo.ListeningVO;
import com.yohann.exam_service.service.ListeningService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 听力表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-11-19
 */
@Api(tags = "听力")
@RestController
@RequestMapping("/exam_service/listening")
public class ListeningController {
    @Autowired
    private ListeningService listeningService;

    /**
     * 添加听力题目
     *
     * @param listening 题目信息
     * @return 添加状态
     */
    @ApiOperation("添加听力题目")
    @PostMapping("addListening")
    public Result addListening(@ApiParam("题目信息") @RequestBody Listening listening) {
        listening.setCreateTime(new Date());
        boolean save = listeningService.save(listening);

        return Result.state(save);
    }

    /**
     * 删除听力题目
     *
     * @param id 题目id
     * @return 删除状态
     */
    @ApiOperation("删除听力题目")
    @GetMapping("deleteListening/{id}")
    public Result deleteListening(@ApiParam("题目id") @PathVariable int id) {
        boolean delete = listeningService.deleteListening(id);

        return Result.state(delete);
    }

    /**
     * 获取听力题目详情
     *
     * @param id 题目id
     * @return 详情信息
     */
    @ApiOperation("获取听力题目详情")
    @GetMapping("getListening/{id}")
    public Result getListening(@ApiParam("题目id") @PathVariable int id) {
        Listening listening = listeningService.getById(id);

        return Result.ok().data("item", listening);
    }

    /**
     * 获取听力题目详情
     *
     * @param id 题目id
     * @return 详情信息
     */
    @ApiOperation("获取听力题目详情")
    @GetMapping("getListeningDetail/{id}")
    public Result getListeningDetail(@ApiParam("题目id") @PathVariable int id) {
        ListeningVO listening = listeningService.getListeningDetail(id);

        return Result.ok().data("item", listening);
    }

    /**
     * 分页查询听力题目
     *
     * @param current 起始位置
     * @param limit   结束位置
     * @param listening   题目标题
     * @return 结果集
     */
    @ApiOperation("分页查询听力题目")
    @PostMapping("query/{current}/{limit}")
    public Result pageQueryListen(@ApiParam("起始位置") @PathVariable int current,
                                  @ApiParam("结束位置") @PathVariable int limit,
                                  @ApiParam("题目标题") @RequestBody(required = false) Listening listening) {
        Map<String, Object> map = listeningService.pageQueryListen(current, limit, Objects.isNull(listening) ? null : listening.getTitle());

        return Result.ok().data(map);
    }

    /**
     * 查询听力题目
     *
     * @param parentId 题目父id
     * @param section  题目章节
     * @return 结果集
     */
    @ApiOperation("查询听力题目")
    @GetMapping("queryListening/{parentId}")
    public Result queryListening(@ApiParam("题目父id") @PathVariable Integer parentId, @ApiParam("题目章节") @RequestParam(value = "section", required = false) Integer section) {
        Result result = listeningService.queryListening(parentId, section);

        return result;
    }

    /**
     * 修改听力题目
     *
     * @param listening 题目信息
     * @return 修改状态
     */
    @ApiOperation("修改听力题目")
    @PostMapping("updateListening")
    public Result updateListening(@ApiParam("题目信息") @RequestBody Listening listening) {
        boolean update = listeningService.updateById(listening);

        return Result.state(update);
    }
}

